package com.gitee.xhs.saveAndModifyingPointWriter.keyedState.valueState;

import com.gitee.xhs.keyedState.valueState.ValueStateConstant;
import com.gitee.xhs.sampleRecod.transactionRecord.Transaction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.state.api.functions.KeyedStateBootstrapFunction;

/**
 * @author BigHead
 * @date 2022/7/12 11:22
 * @description ValueStateNewSavePointsFunction
 */
public class ValueStateNewSavePointsFunction extends KeyedStateBootstrapFunction<Long, Transaction> {

    private transient ValueState<Integer> valueState;

    @Override
    public void open(Configuration configuration) throws Exception {
        valueState = getRuntimeContext().getState(
                ValueStateConstant.VALUE_STATE_DESCRIPTOR
        );
    }

    @Override
    public void processElement(Transaction value, KeyedStateBootstrapFunction<Long, Transaction>.Context ctx) throws Exception {
        Integer stateInteger = 0;

        if (null != valueState.value()) {
            stateInteger = valueState.value();
        }
        stateInteger += 1;
        valueState.update(stateInteger);
    }
}
